<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="楼栋" prop="buildingName">
                <el-select v-model="queryParams.buildingName" placeholder="请选择楼栋" style="width:200px">
                    <el-option v-for="dict in dict.type.loudong" :key="dict.value" :label="dict.label"
                        :value="dict.value"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['business:dormitory:add']">新增</el-button>
            </el-col>
             <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['business:dormitory:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['business:dormitory:remove']">删除</el-button>
            </el-col> 
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['business:dormitory:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <el-table v-loading="loading" :data="dormitoryList" @selection-change="handleSelectionChange" border>
            <el-table-column label="楼栋" align="center" prop="buildingName" width="100px" />

            <el-table-column label="宿舍编号" align="center" prop="dormitorycode" width="100px" />
            <el-table-column label="照片" align="center" prop="picurl">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handlePic(scope.row)">查看照片</el-button>
                </template>
            </el-table-column>
            <el-table-column label="退宿员工" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleDetail(scope.row)">查看详情</el-button>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-date" @click="handleHis(scope.row)">抄电历史</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改退宿舍对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="宿舍id" prop="dormitoryid">
                    <el-input v-model="form.dormitoryid" placeholder="请输入宿舍id" />
                </el-form-item>
                <el-form-item label="抄表时间" prop="readDate">
                    <el-date-picker clearable v-model="form.readDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择抄表时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="度数" prop="numberdegrees">
                    <el-input v-model="form.numberdegrees" placeholder="请输入度数" />
                </el-form-item>
                <!-- <el-form-item label="照片路径" prop="picurl">
                    <el-input v-model="form.picurl" placeholder="请输入照片路径" />
                </el-form-item>
                <el-form-item label="退宿员工ids" prop="employeeids">
                    <el-input v-model="form.employeeids" placeholder="请输入退宿员工ids" />
                </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 抄表历史 -->
        <el-dialog :title="histitle" :visible.sync="hisopen" width="800px" append-to-body>
            <el-table v-loading="hisloading" :data="hisdegreesList" border>
                <el-table-column label="抄表时间" align="center" prop="readDate">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.readDate, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="度数" align="center" prop="numberdegrees" />
                <el-table-column label="上次抄表时间" align="center" prop="lastReadDate">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.lastReadDate, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="差值" align="center" prop="difference" />
            </el-table>
            <pagination v-show="histotal > 0" :total="histotal" :page.sync="hisqueryParams.pageNum"
                :limit.sync="hisqueryParams.pageSize" @pagination="getHisList" />
        </el-dialog>
        <!-- 员工信息详情 -->
        <el-dialog title="员工信息详情" :visible.sync="detailopen" width="1300px" :key="Math.random()" append-to-body>
            <detail :rowData="dynamicData"></detail>
        </el-dialog>
        <!-- 照片 -->
        <el-dialog title="照片" :visible.sync="picopen" width="800px" :key="Math.random()" append-to-body>
            <div class="image-container">
                <div v-for="(src, index) in picurls" :key="index" class="image-wrapper">
                    <el-image style="width: 150px; height: 150px" :src="src" :preview-src-list="picurls">
                    </el-image>
                </div>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listDormitory, getDormitory, delDormitory, addDormitory, updateDormitory, getEmployeeInfoByid } from "@/api/business/hisdormitory";
import { listHisDegrees } from "@/api/business/degrees";
import Detail from "@/views/business/employee/detail"
export default {
    name: "HisDormitory",
    dicts: ['loudong'],

    components: { Detail },
    data() {
        return {
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 退宿舍表格数据
            dormitoryList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                dormitoryid: undefined,
                picurl: undefined,
                employeeids: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {

                dormitoryid: [
                    { required: true, message: "宿舍id不能为空", trigger: "blur" }
                ],
                readDate: [
                    { required: true, message: "时间不能为空", trigger: "blur" }
                ],
                numberdegrees: [
                    { required: true, message: "度数不能为空", trigger: "blur" }
                ]
            },
            // 遮罩层
            hisloading: false,
            // 弹出层标题
            histitle: "",
            // 是否显示弹出层
            hisopen: false,
            // 宿舍电度数表格数据
            hisdegreesList: [],
            histotal: 0,
            // 查询参数
            hisqueryParams: {
                pageNum: 1,
                pageSize: 10,
                dormitoryid: null,

            },
            //员工列表数据
            dynamicData: [],
            detailopen: false,
            picopen: false,
            picurls: [],
         
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询退宿舍列表 */
        getList() {
            this.loading = true;
            listDormitory(this.queryParams).then(response => {
                this.dormitoryList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                dormitoryid: undefined,
                picurl: undefined,
                employeeids: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加退宿舍";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.loading = true;
            this.reset();
            const id = row.id || this.ids
            getDormitory(id).then(response => {
                this.loading = false;
                this.form = response.data;
                this.open = true;
                this.title = "修改退宿舍";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    if (this.form.id != null) {
                        updateDormitory(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        addDormitory(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除退宿舍编号为"' + ids + '"的数据项？').then(() => {
                this.loading = true;
                return delDormitory(ids);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            }).finally(() => {
                this.loading = false;
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/dormitory/export', {
                ...this.queryParams
            }, `dormitory_${new Date().getTime()}.xlsx`)
        },
        handleHis(row) {
            this.hisopen = true;
            this.shititle = "历史记录";
            this.hisqueryParams.dormitoryid = row.dormitoryid;
            this.getHisList();
        },
        getHisList() {
            this.hisloading = true;
            listHisDegrees(this.hisqueryParams).then(response => {
                this.hisdegreesList = response.rows;
                this.histotal = response.total;
                this.hisloading = false;
            });
        },
        //员工详情
        handleDetail(row) {
            getEmployeeInfoByid(row.id).then(response => {
                this.dynamicData = response.data;
                this.detailopen = true;
            });
        },
        //查看照片
        handlePic(row) {
            getDormitory(row.id).then(response => {
                // console.log(response.data.picurl)
                const picurlData = response.data.picurl.replace("[", "").replace("]", "").trim().split(',');
                this.picopen = true;
                this.picurls = [];
                let basepicurl = process.env.VUE_APP_BASE_API.replace("/dev-api", "");
                picurlData.forEach((element) => {
                    // this.picurls.push({
                    //     src: basepicurl + element,
                    // });
                    this.picurls.push(
                        basepicurl + element,
                    );
                });
                //console.log(this.picurls)
            });
        },
    }
};
</script>
<style>
.image-container {
    display: flex;
}

.image-wrapper {
    margin-right: 10px;
    /* Optional: Add some spacing between the images */
}
</style>